Scala で open class
暗默の型變換 (inplicit conversion) で簡單
code:scala
class A {}
implicit class AWithB(a: A) {
def b: A = {
processSomething()
a
}
}
new A().b
冗長には
code:scala
import scala.language.implicitConversions
class A {}
implicit def aAsAWithB(a: A): AWithB = new AWithB(a)
class AWithB(a: A) {
def b: A = {
processSomething()
a
}
}
new A().b